import 'package:flutter/material.dart';
import 'package:flutter_applearnprogrom/Application.dart';
import 'package:flutter_applearnprogrom/HomePage.dart';
import 'package:flutter_applearnprogrom/IndexPage.dart';
import 'package:flutter_applearnprogrom/global/Global.dart';
import 'package:flutter_applearnprogrom/utils/UserToastutils.dart';
import 'package:flutter_applearnprogrom/utils/rsa_utils.dart';
import 'package:flutter_applearnprogrom/base/view.dart';
import 'package:shared_preferences/shared_preferences.dart';

class LoginView extends StatefulWidget {
  @override
  _LoginViewState createState() => _LoginViewState();
}

class _LoginViewState extends State<LoginView> {
  TextEditingController _user;
  TextEditingController _pass;

  @override
  void initState() {
    super.initState();
    print(encodeString("123456")); //  引用加密代码
    _user = new TextEditingController();
    _pass = new TextEditingController();
    loadData();
  }

  @override
  void dispose() {
    _user.dispose();
    _pass.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: getAppbar("登陆"),
      body: Center(
        child: Padding(
          padding: EdgeInsets.all(20),
          child: Column(
            children: <Widget>[
              Image.asset(
                "images/main.jpg",
                width: double.infinity,
                height: 250,
                fit: BoxFit.fill,
              ),
              SizedBox(
                height: 16,
              ),
              TextField(
                decoration: InputDecoration(
                  labelText: "账号",
                  hintText: "请输入账号",
                  prefixIcon: Icon(Icons.person),
                ),
                controller: _user,
                autofocus: true,
                textInputAction: TextInputAction.next,
              ),
              TextField(
                decoration: InputDecoration(
                  labelText: "密码",
                  hintText: "请输入密码",
                  prefixIcon: Icon(Icons.lock),
                ),
                controller: _pass,
                obscureText: true,
                textInputAction: TextInputAction.send,
                onSubmitted: (String) {
                  print("submit");
                  Application.router.navigateTo(context,"/b"); //b为配置路由
                },
              ),
              SizedBox(height: 16.0),
              GestureDetector(
                child: Container(
                  width: double.infinity,
                  child: Text(
                    "找回密码",
                    style: TextStyle(color: Colors.blue),
                    textAlign: TextAlign.right,
                  ),
                ),
                onTap: () {
                  print("找回密码");
                },
              ),
              SizedBox(height: 16.0),
              Container(
                width: double.infinity,
                child: RaisedButton(
                  child: Text("登陆"),
                  onPressed: _login,
                ),
              ),
              SizedBox(height: 8.0),
              Container(
                  width: double.infinity,
                  child: RaisedButton(
                    child: Text("注册"),
                    onPressed: _register,
                  ))
            ],
          ),
        ),
      ),
    );
  }
  void _login() async {
    print("请登录");
    if (_user == null || _user.text.isEmpty) {
//    EasyLoading.showError('账号不能为空！');
      UserToastutils.toast(context, "账号不能为空");
      return;
    } else if (_pass == null || _pass.text.isEmpty) {

      void _addOverlay(BuildContext context) async {
        //获取OverlayState
        UserToastutils.toast(context, "密码不能为空");
        return;
    }
//  context.read<LoginViewmodel>().login(_user.text, _pass.text);
  }else
    {
//      _login();
      _saveLoginMsg();
//      _void_pageintent();
      showDialog(
          context: context,
          builder: (context) {
//            if (_userName.text == "admin" && _userPwd.text == "123456") {
//              String sucess = "登录成功 \n" + _userName.text;
//
//              return AlertDialog(
//                content: Text(sucess),
//              );
//            } else {
//              String err = "登录失败 \n 账号或密码错误";
//              return AlertDialog(
//                content: Text(err),
//              );
//            }
          });
      Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
        return new IndexPage();
      }));
    }
  }


  void _register() async {
    print("请登录");
    dynamic params = await Navigator.of(context).pushNamed("register");
    if (params != null) {
      setState(() {
        _user.text = params["user"];
        _pass.text = params["pass"];
      });
    }
}
  // 保存账号密码
  void _saveLoginMsg() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setString("name", _user.text);
    preferences.setString("pwd", _pass.text);
  }

  // 读取账号密码，并将值直接赋给账号框和密码框
  void _getLoginMsg() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    _user.text = preferences.get("name");
    _pass.text = preferences.get("pwd");
  }
void loadData() async {
  SharedPreferences sp = await SharedPreferences.getInstance();
  String token = await sp.getString("token");
//    print(token);
//    if (token != null) {
//      Global.getInstance().dio.options.headers["token"] = token;
//      context.read<LoginViewmodel>().tokenLogin(token);
//    }
}



}
